//
//  UIView+Extension.swift
//  weibo
//
//  Created by Corgi on 17/3/20.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

extension UIImage {

    class func getScreenSnap() -> UIImage? {
        
        let window = UIApplication.shared.keyWindow!
        
        UIGraphicsBeginImageContext(window.bounds.size)
        
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image
    }

    func dealImageScale( maxWidth: CGFloat) -> UIImage{
        
        if self.size.width < maxWidth {
            
            return self
        }
        
        let height = self.size.height * maxWidth / self.size.width
        
        let rect = CGRect(x: 0, y: 0, width: maxWidth, height: height)
        
        UIGraphicsBeginImageContext(rect.size)
        
        self.draw(in: rect)
        
        let img = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return img!
    }
    
}

extension UIImageView {

    convenience init(imageName: String) {
    
        self.init(image:UIImage(named: imageName))
    
    }
    
    func corgi_setImage(urlString: String?, placeholderImageName: String?) {
        self.sd_setImage(with: URL(string: urlString ?? ""), placeholderImage: UIImage(named: placeholderImageName ?? ""))
    }
}

extension UILabel {

    convenience init(title: String, textColor: UIColor, fontSize: CGFloat) {
    
        self.init()
        
        self.text = title
        
        self.textColor = textColor
            
        self.font = UIFont.systemFont(ofSize: fontSize)
        
        self.numberOfLines = 0
        
        self.textAlignment = .center
        
        self.sizeToFit()
        
    }
}

extension UIButton {

    convenience init(title: String, textColor: UIColor, fontSize: CGFloat, backImageName: String, imageName: String? = nil) {
    
        self.init()
    
        self.setTitle(title, for: .normal)
        
        self.setTitleColor(textColor, for: .normal)
        
        self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
    
        self.setBackgroundImage(UIImage(named: backImageName), for: .normal)
        
        if let imgName = imageName {
            
            self.setImage(UIImage(named:imgName), for: .normal)
            
        }
    }
}








